package com.lzx.applib.widget.image.imageview_attacher;
import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.view.ScaleGestureDetector;
import android.view.ScaleGestureDetector.OnScaleGestureListener;


public	class MScaleListener implements OnScaleGestureListener {
		
		private ImageViewAttacher attacher;
		CalcScaleHelper mCalcScaleHelper;
		
		
		float lastSpan = 0;

		public static final float MAX_SCALE = 6;
		public static final float MIN_SCALE = 1;

		

		public MScaleListener(ImageViewAttacher attacher) {
			// TODO Auto-generated constructor stub
			this.attacher = attacher;
			mCalcScaleHelper = new CalcScaleHelper();
		}
		
		
		@SuppressLint("NewApi") @Override
		public boolean onScale(ScaleGestureDetector detector) {
			// TODO Auto-generated method stub
			attacher.log("OnScaleGestureListener : onScale:" + detector.getScaleFactor() + "  " + detector.getCurrentSpan());
			attacher.log("OnScaleGestureListener : onScale:" + "getPreviousSpan" + detector.getPreviousSpan() + "; getCurrentSpan" + detector.getCurrentSpan());

			float f = detector.getScaleFactor();
			f = mCalcScaleHelper.add(f);
			f = f*oldScale;


//			float f = detector.getCurrentSpan()/lastSpan;
//			lastSpan = detector.getCurrentSpan();
			
			

			attacher.log("OnScaleGestureListener: factor:" + f);
			attacher.setScale(f);
			
			return false;
		}

		
		float oldScale = 1;
		@SuppressLint("NewApi") @Override
		public boolean onScaleBegin(ScaleGestureDetector detector) {
			// TODO Auto-generated method stub
			attacher.log("OnScaleGestureListener : onScaleBegin :" + detector.getScaleFactor() + "  " + detector.getCurrentSpan());;
			oldScale = attacher.getScale();
			mCalcScaleHelper.initScales();
			lastSpan = detector.getCurrentSpan();
			return true;
		}

		@SuppressLint("NewApi") @Override
		public void onScaleEnd(ScaleGestureDetector detector) {
			// TODO Auto-generated method stub
			attacher.log("OnScaleGestureListener : onScaleEnd :" + detector.getScaleFactor() + "  " + detector.getCurrentSpan());
			oldScale = attacher.getScale();
			
//			if (oldScale > MAX_SCALE) {
//				anim(1, MAX_SCALE/oldScale, 1, MAX_SCALE/oldScale, MAX_SCALE);
//			} else if (oldScale < MIN_SCALE) {
//				anim(1, MIN_SCALE/oldScale, 1, MIN_SCALE/oldScale, MIN_SCALE);
//			}
			
			if (oldScale > MAX_SCALE) {
				attacher.anim(oldScale, MAX_SCALE, oldScale, MAX_SCALE, MAX_SCALE);
			} else if (oldScale < MIN_SCALE) {
				attacher.anim(oldScale, MIN_SCALE, oldScale, MIN_SCALE, MIN_SCALE);
			}
		}
	}
	
	class CalcScaleHelper {
		
		public ArrayList<Float> scales = new ArrayList<Float>();
		public int length = 5;
		
		public CalcScaleHelper() {
			// TODO Auto-generated constructor stub
			initScales();
		}
		
		public void initScales() {
			scales = new ArrayList<Float>();
			for (int i = 0; i < length; i++) {
				scales.add(1f);
			}
		}
		
		public float add(float f) {
			scales.remove(0);
			scales.add(f);
			return getAvarge();
		}
		
		public float getAvarge() {
			float scale = 0;
			for (int i = 0; i < length; i++) {
				scale += scales.get(i);
			}
			scale /= length;
			return scale;
		}

	}